package com.burgeon.backend.SHOPBACKEND.rest;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.ShopMail;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class QueryMail extends RESTBase {
    private String ip;
    private String account;
    private String password;

    public QueryMail(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public ArrayList<ShopMail> getData(String name) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(name), url);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        ArrayList<ShopMail> mailArrayList = new ArrayList<ShopMail>();

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONArray array = jsonArray.getJSONArray(i);

            ShopMail forder = new ShopMail();

            //forder.setId(jsonArray.getJSONArray(i).getString(0));
            forder.setOid(jsonArray.getJSONArray(i).getString(0));
            //forder.setCode(jsonArray.getJSONArray(i).getString(2));
            forder.setName(jsonArray.getJSONArray(i).getString(1));

            mailArrayList.add(forder);
        }
        return mailArrayList;
    }

    private MultiValueMap Data(String name) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");

        columns.add("CODE");//
        //columns.add("NAME");//
        //columns.add("PRICE");//

        params.put("columns", columns);
        params.put("table", "C_EXPRESSCOMPANY");//表名

        params.put("count","true");

        HashMap paramss = new HashMap();//DOCNO  (AK)	单据编号
        String ac = "'="+name+"'";
        paramss.put("column", "'NAME'");
        paramss.put("condition", ac);
        params.put("params", paramss);

        return DATA("Query", params,account,password);
    }

    public static void main(String[] args) {
        QueryMail queryMail = new QueryMail("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(queryMail.getData("顺丰"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
